package com.ebelter.btcomlib.views;

import android.content.Context;
import android.content.res.TypedArray;

import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;


import com.ebelter.btcomlib.R;


/**
 * Created by ZCQ on 2017/12/1
 * 组合控件1 不知道怎么起名字了 还是按序号来吧
 */

public class CombinView1 extends LinearLayout{
    public CombinView1(Context context) {
        this(context,null);
    }


    private int lineColor=0xffffff;
    private int hintColor=0xffffff;
    private int textColor=0xffffff;

    public CombinView1(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }




    public CombinView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CombinView1);
        if (typedArray != null) {
            title1 = typedArray.getString(R.styleable.CombinView1_title1);
            hint1 = typedArray.getString(R.styleable.CombinView1_hint1);
            inputStyle = typedArray.getInt(R.styleable.CombinView1_inputStyle,0);

            lineColor = typedArray.getColor(R.styleable.CombinView1_linecolor,lineColor);//xy坐标轴颜色
            hintColor = typedArray.getColor(R.styleable.CombinView1_hintcolor,hintColor);//xy坐标轴颜色
            textColor = typedArray.getColor(R.styleable.CombinView1_textcolor,textColor);//xy坐标轴颜色
            typedArray.recycle();
        }

        initView(getContext());


    }

    private String title1;
    private String hint1;
    private int inputStyle;

    private EditText title1_ed;
    private View lineView;

//
//    Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        mPaint.setColor(Color.parseColor("#ff0000"));
//        mPaint.setTextSize(18);
//        canvas.drawText("123",getMeasuredWidth()/2,getMeasuredHeight()/2,mPaint);
//    }

    private void initView(Context context) {
        View view =  View.inflate(context, R.layout.cb1_layout, this);//把那个布局当做是自己的孩子

        LinearLayout root = view.findViewById(R.id.cb1_root_ll);
        lineView = view.findViewById(R.id.cb1_line_v);

        title1_ed = view.findViewById(R.id.cb1_ed);

        title1_ed.setTextColor(textColor);
        title1_ed.setHintTextColor(hintColor);
        lineView.setBackgroundColor(lineColor);

        setTitle1(title1);
        setHint1(hint1);
        setInputStyle();
    }

    private void setInputStyle() {
        if (inputStyle==1){
            title1_ed.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }

    }

    public String getTitle1(){
        return title1_ed.getText().toString().trim();
    }



    public void setTitle1(String title1){
        this.title1=title1;
        if (title1_ed!=null){
            title1_ed.setText(this.title1);
        }
    }


    public void setHint1(String hint1) {
        this.hint1 = hint1;
        if (title1_ed!=null){
            title1_ed.setHint(hint1);
        }
    }

    //0phone 1text 2number
    public void setTittle1InputType(int model) {
        if (title1_ed!=null){
            if (model==0){
                title1_ed.setInputType(InputType.TYPE_CLASS_PHONE);
            }else if (model==1){
                title1_ed.setInputType(InputType.TYPE_CLASS_TEXT);
            }else if (model==2){
                title1_ed.setInputType(InputType.TYPE_CLASS_NUMBER);
            }else if (model==3){
                title1_ed.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
            }


        }
    }

    public void setSelection(int length) {
        title1_ed.setSelection(length);
    }
}
